﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class CityInfo : Form
    {
        private int cityId = 0;

        public CityInfo(int id)
        {
            InitializeComponent();
            cityId = id;
        }

        private void CityInfo_Load(object sender, EventArgs e)
        {
            if (cityId != 0) {

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[getCity]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = cityId;
                SqlDataReader dataReader = myCommand.ExecuteReader();
                if (dataReader.Read())
                {
                    tbName.Text = Convert.ToString(dataReader["name"]);
                }
                dataReader.Close();
                conn.Close();
            }
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            if (tbName.Text.Length > 0)
            {
                save.Enabled = true;
            }
            else {
                save.Enabled = false;
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (cityId != 0)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[editCity]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = cityId;
                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, tbName.Text.Length).Value = tbName.Text;
                SqlDataReader dataReader = myCommand.ExecuteReader();
                if (dataReader.Read())
                {
                    tbName.Text = Convert.ToString(dataReader["name"]);
                }
                dataReader.Close();
                conn.Close();
            }
            else
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[addCity]";
                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, tbName.Text.Length).Value = tbName.Text;
                cityId = Convert.ToInt32(myCommand.ExecuteScalar());
                conn.Close();
            }

            this.Close();
        }
    }
}
